﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EXT;
using DevExpress.XtraReports.UI;
using DevExpress.Utils;

namespace BanHangCshap.DanhMuc.gkhtdl63.Tonghop
{
    public partial class frmLichsuGiaodichChitiet : Form
    {
        public frmLichsuGiaodichChitiet()
        {
            InitializeComponent();
        }

        DataTable xgDTKhachhang2 = new DataTable();

        private void frmLichsuGiaodichChitiet_Load(object sender, EventArgs e)
        {
            dtpTungay.EditValue = SDN.LayNgaydauthang();
            dtpDenngay.EditValue = DateTime.Now;
            Load_Khachang2();
        }

        DataTable xgDTGDKhach = new DataTable();

        Class.gkhtdl63.BLL.AllInOneBLL xgAllInOneBLL = new BanHangCshap.Class.gkhtdl63.BLL.AllInOneBLL();


        private void GDKhachHang()
        {
            long Id_Khachhang2 = (long)gleKhachhang2.EditValue.ToFloat();
            xgDTGDKhach = xgAllInOneBLL.Khachhang2_Intongtop2(dtpTungay.EditValue, dtpDenngay.EditValue, Id_Khachhang2);
            DataColumn xlDC = new DataColumn("NoHientai", typeof(Int64));
            DataColumn xlDC1 = new DataColumn("LoaiGiaodich", typeof(string));
            DataRow[] xx = xgDTKhachhang2.Select(String.Format("Id = '{0}'", Id_Khachhang2));

            xgDTGDKhach.Columns.Add(xlDC);
            xgDTGDKhach.Columns.Add(xlDC1);

            DataRow dr = xgDTGDKhach.NewRow();

            dr[0] = DBNull.Value;
            dr[1] = DBNull.Value;
            dr[2] = "Công nợ ban đầu";
            dr[3] = xgAllInOneBLL.Phieuxuat2_TinhNocuKhachhangK(dtpTungay.EditValue, Id_Khachhang2).Rows[0][2]; ;//xx[0]["CongnoBandau"].ToInt64();
            dr[4] = 0;
            dr[5] = 0;
            dr[6] = 1;
            dr[7] = 0;
            dr[8] = "";

            xgDTGDKhach.Rows.InsertAt(dr, 0);
            int x = xgDTGDKhach.Rows.Count;

            Int64 Val = 0;
            for (int i = 0; i < x; i++)
            {
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 1)
                {
                    Val += xgDTGDKhach.Rows[i]["Tongtien"].ToInt64() - xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                    if (!xgDTGDKhach.Rows[i][0].ToString().Trim().Equals(""))
                    {
                        xgDTGDKhach.Rows[i][8] = "Xuất hàng";
                    }
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 3)
                {
                    Val = Val - xgDTGDKhach.Rows[i]["Tongtien"].ToInt64() + xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                    xgDTGDKhach.Rows[i][8] = "Khách trả hàng";
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 5)
                {
                    Val = Val - xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDKhach.Rows[i][8] = "Phiếu thu";
                }
                if (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 6)
                {
                    Val = Val + xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                    xgDTGDKhach.Rows[i][8] = "Phiếu chi";
                }
                xgDTGDKhach.Rows[i][7] = Val;
            }

            DataTable xlDTGDChitiet = xgAllInOneBLL.TonghopGiaodichKhach(dtpTungay.EditValue, dtpDenngay.EditValue, Id_Khachhang2);
            int xy = xlDTGDChitiet.Rows.Count;

            DataTable KQ = new DataTable();
            DataColumn xlNgay = new DataColumn("Ngay", typeof(DateTime));
            DataColumn xlMa = new DataColumn("Ma", typeof(string));
            DataColumn xlTongtien = new DataColumn("Tongtien", typeof(Int64));
            DataColumn xlThanhtoan = new DataColumn("Thanhtoan", typeof(Int64));
            DataColumn xlCKP = new DataColumn("CKP", typeof(Int64));
            DataColumn xlNoHientai = new DataColumn("NoHientai", typeof(Int64));
            DataColumn xlLoaiGiaodich = new DataColumn("LoaiGiaodich", typeof(string));
            DataColumn xlDVT = new DataColumn("DVT", typeof(string));
            DataColumn xlSoluong = new DataColumn("Soluong", typeof(Int64));
            DataColumn xlDongia = new DataColumn("Dongia", typeof(Int64));
            DataColumn xlCK = new DataColumn("CK", typeof(Int64));
            DataColumn xlThanhtien = new DataColumn("Thanhtien", typeof(Int64));
            DataColumn xlTen = new DataColumn("Ten", typeof(string));
            DataColumn xlId = new DataColumn("Id", typeof(int));
            
            KQ.Columns.Add(xlNgay);
            KQ.Columns.Add(xlMa);
            KQ.Columns.Add( xlTongtien);
            KQ.Columns.Add( xlThanhtoan);
            KQ.Columns.Add( xlCKP);
            KQ.Columns.Add( xlNoHientai);
            KQ.Columns.Add( xlLoaiGiaodich);
            KQ.Columns.Add( xlDVT);
            KQ.Columns.Add( xlSoluong);
            KQ.Columns.Add( xlDongia);
            KQ.Columns.Add(xlCK);
            KQ.Columns.Add( xlThanhtien);
            KQ.Columns.Add( xlTen);
            KQ.Columns.Add(xlId);

            for (int i = 0; i < x; i++)
            {
                bool Dathem = true;
                for (int j = 0; j < xy; j++)
                {
                    if (xgDTGDKhach.Rows[i]["Id"].ToInt64() == xlDTGDChitiet.Rows[j]["Id_Phieuxuat2"].ToInt64() && (xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 1 || xgDTGDKhach.Rows[i]["Loai"].ToInt64() == 3))
                    {
                        Dathem = false;
                        DataRow dr1 = KQ.NewRow();
                        dr1[0] = xgDTGDKhach.Rows[i]["Ngay"].ToDateTime();
                        dr1[1] = xgDTGDKhach.Rows[i]["Ma"].ToString();
                        dr1[2] = xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                        dr1[3] = xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                        dr1[4] = xgDTGDKhach.Rows[i]["CK"].ToInt64();
                        dr1[5] = xgDTGDKhach.Rows[i]["NoHientai"].ToInt64();
                        dr1[6] = xgDTGDKhach.Rows[i]["LoaiGiaodich"].ToString();

                        dr1[7] = xlDTGDChitiet.Rows[j]["DVT"].ToString();
                        dr1[8] = xlDTGDChitiet.Rows[j]["Soluong"].ToInt64();
                        dr1[9] = xlDTGDChitiet.Rows[j]["Dongia"].ToInt64();
                        dr1[10] = xlDTGDChitiet.Rows[j]["CK"].ToInt64();
                        dr1[11] = xlDTGDChitiet.Rows[j]["Thanhtien"].ToInt64();
                        dr1[12] = xlDTGDChitiet.Rows[j]["Ten"].ToString();
                        dr1[13] = i;
                        KQ.Rows.Add(dr1);
                    }
                }
                if (Dathem)
                {
                    DataRow dr2 = KQ.NewRow();
                    if (!xgDTGDKhach.Rows[i]["Ngay"].ToString().Trim().Equals(""))
                    {
                        dr2[0] = xgDTGDKhach.Rows[i]["Ngay"].ToDateTime();
                    }
                    dr2[1] = xgDTGDKhach.Rows[i]["Ma"].ToString();
                    dr2[2] = xgDTGDKhach.Rows[i]["Tongtien"].ToInt64();
                    dr2[3] = xgDTGDKhach.Rows[i]["Thanhtoan"].ToInt64();
                    dr2[4] = xgDTGDKhach.Rows[i]["CK"].ToInt64();
                    dr2[5] = xgDTGDKhach.Rows[i]["NoHientai"].ToInt64();
                    dr2[6] = xgDTGDKhach.Rows[i]["LoaiGiaodich"].ToString();
                    dr2[13] = i;
                    KQ.Rows.Add(dr2);
                }
            }

            DataRow[] XYZ = xgDTKhachhang2.Select("Id = " + gleKhachhang2.EditValue.ToInt32() + "");
            Report.gkhtdl63.MyRPT.Thongke.rptLichsuGiaoDichChitiet2 rpt = new BanHangCshap.Report.gkhtdl63.MyRPT.Thongke.rptLichsuGiaoDichChitiet2(XYZ[0]["Khachhang2"].ToString(), XYZ[0]["SoDienthoai"].ToString(), XYZ[0]["Diachi"].ToString(),"Từ ngày "+ dtpTungay.Text+" đến ngày "+dtpDenngay.Text);
            rpt.DataSource = KQ;
            SetReport(rpt);
        }

        private void Load_Khachang2()
        {
            Class.gkhtdl63.BLL.Khachhang2BLL xlKhachhang2BLL = new BanHangCshap.Class.gkhtdl63.BLL.Khachhang2BLL();
            xgDTKhachhang2 = xlKhachhang2BLL.Khachhang2_Laydanhsach_View(1);
            if (xgDTKhachhang2 != null)
            {
                DataRow dr = xgDTKhachhang2.NewRow();
                dr[0] = DBNull.Value;
                dr[1] = DBNull.Value;
                dr[2] = DBNull.Value;
                dr[3] = DBNull.Value;
                xgDTKhachhang2.Rows.InsertAt(dr, 0);
                gleKhachhang2.Properties.DataSource = xgDTKhachhang2;
            }
            else
                DevExpress.XtraEditors.XtraMessageBox.Show("Không kết nối được với CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void gleKhachhang2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index==1)
            {
                WaitDialogForm dg = new WaitDialogForm("Đang load dữ liệu...", "Xin chờ trong giây lát.");
                GDKhachHang();
                dg.Close();
            }
        }

        public void SetReport(XtraReport r)
        {
            printControl1.PrintingSystem = r.PrintingSystem;
            r.CreateDocument();
            printControl1.UpdatePageView();
        }
    }
}
